Numpy

# Uncomment and run this cell to install numpy
# !pip install numpy

Inspecting our arrays

# import numpy module
import numpy as np
# checking the numpy version
np.__version__
'1.24.1'
# creating a numpy array
num_arr = np.array([1, 2, 3, 4])

The object that’s created by array() is called ndarray. This can be shown by checking the type of the object using type()

# Checking type of object
type(num_arr)
numpy.ndarray

Data Types

The table below describes some of the most common data types we use in numpy

Data Type Description
np.int64 Signed 64-bit integer types
np.float32 Standard double-precision floating point
np.complex Complex numbers represented by 128 floats
np.bool Boolean type storing True and False values
np.object Python object type
np.string_ Fixed-length string type
np.unicode_ Fixed-length unicode type
# shape of array
num_arr.shape
(4,)
# finding the number of dimensions
num_arr.ndim
1
# number of elements in array
len(num_arr)
4
# another way to get the number of elements
num_arr.size
4
# finding data type of array elements
num_arr.dtype.name
'int64'
# converting an array
float_arr = np.array([1.2, 3.5, 7.0])

# use astype() to convert to a specific
int_arr = float_arr.astype(int)

print(f'Array: {float_arr}, Data Type: {float_arr.dtype}')
print(f'Array: {int_arr}, Data Type: {int_arr.dtype}')
Array: [1.2 3.5 7. ], Data Type: float64
Array: [1 3 7], Data Type: int64

Ask for help

np.info(np.ndarray.shape)
Tuple of array dimensions.

The shape property is usually used to get the current shape of an array,
but may also be used to reshape the array in-place by assigning a tuple of
array dimensions to it.  As with `numpy.reshape`, one of the new shape
dimensions can be -1, in which case its value is inferred from the size of
the array and the remaining dimensions. Reshaping an array in-place will
fail if a copy is required.

.. warning::

    Setting ``arr.shape`` is discouraged and may be deprecated in the
    future.  Using `ndarray.reshape` is the preferred approach.

Examples
--------
>>> x = np.array([1, 2, 3, 4])
>>> x.shape
(4,)
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)
>>> y.shape = (3, 8)
>>> y
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
>>> y.shape = (3, 6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged
>>> np.zeros((4,2))[::2].shape = (-1,)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Incompatible shape for in-place modification. Use
`.reshape()` to make a copy with the desired shape.

See Also
--------
numpy.shape : Equivalent getter function.
numpy.reshape : Function similar to setting ``shape``.
ndarray.reshape : Method similar to setting ``shape``.
?np.ndarray.shape
Type:        getset_descriptor
String form: <attribute 'shape' of 'numpy.ndarray' objects>
Docstring:  
Tuple of array dimensions.
The shape property is usually used to get the current shape of an array,
but may also be used to reshape the array in-place by assigning a tuple of
array dimensions to it.  As with `numpy.reshape`, one of the new shape
dimensions can be -1, in which case its value is inferred from the size of
the array and the remaining dimensions. Reshaping an array in-place will
fail if a copy is required.
.. warning::
    Setting ``arr.shape`` is discouraged and may be deprecated in the
    future.  Using `ndarray.reshape` is the preferred approach.
Examples
--------
>>> x = np.array([1, 2, 3, 4])
>>> x.shape
(4,)
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)
>>> y.shape = (3, 8)
>>> y
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
>>> y.shape = (3, 6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged
>>> np.zeros((4,2))[::2].shape = (-1,)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Incompatible shape for in-place modification. Use
`.reshape()` to make a copy with the desired shape.
See Also
--------
numpy.shape : Equivalent getter function.
numpy.reshape : Function similar to setting ``shape``.
ndarray.reshape : Method similar to setting ``shape``.

Array mathematics

Arithmetic Operations

# creating arrays
array1 = np.array([1, 4, 6, 7])
array2 = np.array([3, 5, 3, 1])
# subtract
difference1 = array1 - array2
print('difference1 =', difference1)

# another way
difference2 = np.subtract(array1, array2)
print('difference2 =', difference2)
difference1 = [-2 -1  3  6]
difference2 = [-2 -1  3  6]
# sum
summation1 = array1 + array2
print('summation1 =', summation1)

# another way
summation2 = np.add(array1, array2)
print('summation2 =', summation2)
summation1 = [4 9 9 8]
summation2 = [4 9 9 8]

Trigonometric operations

# sin
print('sin(array1) =', np.sin(array1))
# cos
print('cos(array1) =', np.cos(array1))
# log
print('log(array1) =', np.log(array1))
sin(array1) = [ 0.84147098 -0.7568025  -0.2794155   0.6569866 ]
cos(array1) = [ 0.54030231 -0.65364362  0.96017029  0.75390225]
log(array1) = [0.         1.38629436 1.79175947 1.94591015]
# dot product
array1.dot(array2)
48

Research:

  • another way to dot matrices (arrays)

Comparison

array1 == array2
array([False, False, False, False])
array1 > 3
array([False,  True,  True,  True])

Aggregate functions

# average
mean = array1.mean()
print('Mean: ', mean)

# min
minimum = array1.min()
print('Minimum: ', minimum)

# max
maximum = array1.max()
print('Maximum: ', maximum)

# corrcoef
correlation_coefficient = np.corrcoef(array1, array2)
print('Correlation Coefficient: ', correlation_coefficient)

standard_deviation = np.std(array1)
print('Standard Deviation: ', standard_deviation)
Mean:  4.5
Minimum:  1
Maximum:  7
Correlation Coefficient:  [[ 1.         -0.46291005]
 [-0.46291005  1.        ]]
Standard Deviation:  2.29128784747792

Research:

  • copying arrays (you might meet view(), copy())

Subsetting, Slicing and Indexing

  • Indexing is the technique we use to access individual elements in an array. 0 represents the first element, 1 the represents second element and so on.
  • Slicing is used to access elements of an array using a range of two indexes. The first index is the start of the range while the second index is the end of the range. The indexes are separated by a colon ie [start:end]
# Creating numpy arrays of different dimension

arr1 = np.array([1, 4, 6, 7]) # 1D array
print('Array1 (1D): \n', arr1)

arr2 = np.array([[1.5, 2, 3], [4, 5, 6]]) # 2D array
print('Array2 (2D): \n', arr2)

arr3 = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], 
                 [[10, 11, 12], [13, 14, 15], [16, 17, 18]]]) #3D array
print('Array3 (3D): \n', arr3)
Array1 (1D): 
 [1 4 6 7]
Array2 (2D): 
 [[1.5 2.  3. ]
 [4.  5.  6. ]]
Array3 (3D): 
 [[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[10 11 12]
  [13 14 15]
  [16 17 18]]]
# find the dimensions of an array
arr3.shape
(2, 3, 3)

Indexing

# accessing items in a 1D array
arr1[2]
6
# accessing items in 2D array
arr2[0, 2]
3.0
# accessing in a 3D array
arr3[0, 2, 1]
8

slicing

# slicing 1D array
arr1[0:3]
array([1, 4, 6])
# slicing a 2D array
arr2[1, 0:2]
array([4., 5.])
# slicing a 3D array
first = arr3[0, 2]
second = arr3[1, 0]

np.concatenate((first, second))
array([ 7,  8,  9, 10, 11, 12])
# boolean indexing
arr1[arr1 < 5]
array([1, 4])

Research:

  • Fancy Indexing

Array manipulation

print(arr2)
[[1.5 2.  3. ]
 [4.  5.  6. ]]
# transpose
arr2_transpose1 = np.transpose(arr2) 
print('Transpose1: \n', arr2_transpose1)

# another way
arr2_transpose2 = arr2.T
print('Transpose2: \n', arr2_transpose2)
Transpose1: 
 [[1.5 4. ]
 [2.  5. ]
 [3.  6. ]]
Transpose2: 
 [[1.5 4. ]
 [2.  5. ]
 [3.  6. ]]
# combining arrays
first = arr3[0, 2]
second = arr3[1, 0]

np.concatenate((first, second))
array([ 7,  8,  9, 10, 11, 12])

Some homework

Research:

Adding/Removing Elements - resize() - append() - insert() - delete()

Changing array shape - ravel() - reshape()

# stacking 
# np.vstack((a,b))
# np.hstack((a,b))
# np.column_stack((a,b))
# np.c_[a, b]
# splitting arrays
# np.hsplit()
# np.vsplit()
Back to top